﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Timers;
using DLL.Entities;

namespace RAD
{
    public partial class Form1 : Form
    {        

        public Form1()
        {
            InitializeComponent();
        }

        Targets bogie = new Targets();
        WriteXML xWrite = new WriteXML();

        private void Form1_Load(object sender, EventArgs e)
        {
            timer2.Enabled = true;
            timer1.Enabled = true;

            ReadXML xRead = new ReadXML();

            RAD_Config modver = new RAD_Config();
            modver = xRead.ReadRADConfig();

            txtRadModel.Text = Convert.ToString(modver.Model);
            txtRadVersion.Text = Convert.ToString(modver.Version);
            
        }

        string cwd = System.IO.Directory.GetCurrentDirectory();

        private void btnRandom_Click(object sender, EventArgs e)
        {
            Random random = new Random();
            int rand = random.Next(1, 5);

            switch (rand)
            {
                case 1:
                    txtBDirection.Text = "North";
                    break;
                case 2:
                    txtBDirection.Text = "South";
                    break;
                case 3:
                    txtBDirection.Text = "East";
                    break;
                case 4:
                    txtBDirection.Text = "West";
                    break;
                default:
                    break;
            }

            txtBAltitude.Text = Convert.ToString(random.Next(0, 10001));
            txtBSize.Text = Convert.ToString(random.Next(2, 51));
            txtBSpeed.Text = Convert.ToString(random.Next(20, 401));
            txtBDistance.Text = Convert.ToString(random.Next(25, 41));

            if (Convert.ToInt16(txtBSpeed.Text) > 200)
            {
                rbtnMissile.Select();
                txtTargetName.Text = " Missile";
            }
            else if (Convert.ToInt16(txtBAltitude.Text) <= 40 && (Convert.ToInt16(txtBSpeed.Text) > 20))
            {
                rbtnShip.Select();
                txtTargetName.Text = "Ship";
            }
            else if (Convert.ToInt16(txtBAltitude.Text) <= 500 && (Convert.ToInt16(txtBSpeed.Text) <= 20))
            {
                rbtnBird.Select();
                txtTargetName.Text = "Bird";
            }
            else if (Convert.ToInt16(txtBAltitude.Text) > 40)
            {
                rbtnPlane.Select();
                txtTargetName.Text = "Plane";
            }                               
        }  
        

        private void timer1_Tick(object sender, EventArgs e)
        {            
            xWrite.WriteRadUpdate(txtBSpeed.Text, txtBSize.Text, txtBAltitude.Text, txtBDirection.Text, txtBDistance.Text, txtTargetName.Text);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {

            if (File.Exists("../../../DLL/XML/Target_Clear.xml"))
            {
                txtBAltitude.Clear();
                txtBDirection.Clear();
                txtBDistance.Clear();
                txtBSize.Clear();
                txtBSpeed.Clear();
                txtTargetName.Clear();

                File.Delete("../../../DLL/XML/Target_Clear.xml");
            }
        }


   
    }
}
